<?php
YII::import('application.components.bid.class.BidComponent');

class BidAuctionCost extends BidComponent
{

    public $auction_id;
	public $bid_auction_cost;
    
    public function setAucitonId($auctionId){
        $this->auction_id = $auctionId;
        return $this;
    }
    
    public function primaryKey(){
    	return $this->auction_id.':'.$this->getPrototype()->customer_id.':'.$this->getPrototype()->bid_type;
    }
    
    public function cost()
    {
        $amount = abs($this->getPrototype()->getAmount());
        $costSQL = '
            INSERT INTO
                customer_bid_auction_count
            VALUES (
                "' . $this->primaryKey() . '",
                "' . $this->getPrototype()->customer_id . '",
                "' . $this->auction_id . '",
                "' . $this->getPrototype()->bid_type . '",
                @bid_auction_cost := ' . $amount . '
            )
            ON DUPLICATE KEY UPDATE
                customer_bid_auction_count.bid_num = @bid_auction_cost :=  customer_bid_auction_count.bid_num + ' . $amount . '
        ';
        $getBidAuctionCostSQL = '
       		SELECT
        		@bid_auction_cost AS bid_auction_cost
        		
        ';
        $execute = Yii::app()->db->createCommand($costSQL)->execute();
        $getBidAuctionCost = Yii::app()->db->createCommand($getBidAuctionCostSQL)->queryRow();
        $this->bid_auction_cost = $getBidAuctionCost['bid_auction_cost'];
        return $execute;
    }
    
    public function getBidAuctionCost(){
    	return $this->bid_auction_cost;
    }
    
}